package com.mr.modle;

import com.mr.view.BackgroundImage;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

//障碍类
public class Obstacle {
    public int x,y;//纵横坐标
    public BufferedImage image;
    private BufferedImage stone;//石头图片
    private BufferedImage cacti;//仙人掌图片
    private int speed;//移动速度 图片跟着背景走
    public Obstacle(){
        try {
            stone = ImageIO.read(new File("image/石头.png"));
            cacti = ImageIO.read(new File("image/仙人掌.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Random r = new Random();//创建随即对象
        if (r.nextInt(2) == 0){//从0到1中取一值,若为0
            image = cacti;//采用仙人掌图片
        }else{
            image = stone;//否则采用石头图片
        }
        x = 800;//初始横坐标 
        y = 420 - image.getHeight();//纵坐标 使障碍处于地平线上
        speed = BackgroundImage.SPEED;//移动速度与背景同步
    }

//移动
    public void move(){
        x -= speed;//横坐标递减
    }

//获取边界
    public Rectangle getBounds(){
        if (image == cacti){//如果使用仙人掌的图片
            return new Rectangle(x + 7,y,15,image.getHeight());//返回仙人掌的边界

       }
        return new Rectangle(x + 5,y + 4,23,21);//返回石头的边界
    }

//是否存活
    public boolean isLive(){
        if (x <= -image.getHeight()){//如果移出了游戏界面
            return false;//消亡
        }
        return true;//存活
    }
}
